<?php
function getuser(){
	$myfile = fopen("getip.txt", "a") or die("Error");
	$txt = "User-Ip: " . $_SERVER['REMOTE_ADDR'] . " Time: " . date("Y-m-d H:i:s")." User-Agent: " . $_SERVER["HTTP_USER_AGENT"] . " appid: " . $_GET["appid"] . "\n";
	fwrite($myfile, $txt);
	fclose($myfile);
	echo "<h1>操作成功执行。</h1>";
}
function nowapi_call($a_parm){
    if(!is_array($a_parm)){
        return false;
    }
    //combinations
    $a_parm['format']=empty($a_parm['format'])?'json':$a_parm['format'];
    $apiurl=empty($a_parm['apiurl'])?'http://api.k780.com/?':$a_parm['apiurl'].'/?';
    unset($a_parm['apiurl']);
    foreach($a_parm as $k=>$v){
        $apiurl.=$k.'='.$v.'&';
    }
    $apiurl=substr($apiurl,0,-1);
    if(!$callapi=file_get_contents($apiurl)){
        return false;
    }
    //format
    if($a_parm['format']=='base64'){
        $a_cdata=unserialize(base64_decode($callapi));
    }elseif($a_parm['format']=='json'){
        if(!$a_cdata=json_decode($callapi,true)){
            return false;
        }
    }else{
        return false;
    }
    //array
    if($a_cdata['success']!='1'){
        echo $a_cdata['msgid'].' '.$a_cdata['msg'];
        return false;
    }
    return $a_cdata['result'];
}

//https://mp.toutiao.com/mp/agw/statistic/fans_count total toutiao_stat xigua_stat
//echo "<!DOCTYPE html><html><head><meta charset='utf-8'><title>TZTT开放平台-Coding The World.</title></head><body>";
if ("getuser" == $_GET["appid"])
{
	getuser();
}
elseif ("getweather" == $_GET["appid"])
{
	$nowapi_parm['app']='weather.today';
	$nowapi_parm['weaid']=$_GET['city'];
	$nowapi_parm['appkey']='xxxxxxxxxxxxxxxxxxxxxxx';
	$nowapi_parm['sign']='xxxxxxxxxxxxxxxxxxxxxxxxxxb';
	$nowapi_parm['format']='json';
	$result=nowapi_call($nowapi_parm);
	echo "最低温度/最高温度：" . $result["temperature"] . " \n 平均温度：" . $result['temperature_curr'] . " \n 湿度：" . $result['humidity'] . " \n 空气质量指数：" . $result['aqi'] . " \n 天气情况：" . $result['weather'] . " \n " . $result['wind'] . " " . $result['winp'];
}
elseif ("getfans" == $_GET['appid'])
{
	$data = array ('foo' => 'bar');
	$data = http_build_query($data);
	$cookie = 'sid_tt=' . $_GET['sid'];
	$opts = array('http' => array('header'=> 'Cookie:'.@$cookie.''));
	$context = stream_context_create($opts);
	$s = file_get_contents('https://mp.toutiao.com/mp/agw/statistic/fans_count', false, $context);
	$s = json_decode($s, true);
	$fansdate = $s['fans_overview_data']['date'];
	$total = $s['total_fans_count'];
	$toutiao = $s['fans_data']['toutiao_stat'];
	$xigua = $s['fans_data']['xigua_stat'];
	$arr = array('total' => $total, 'toutiao' => $toutiao, 'xigua' => $xigua, 'date' => $fansdate , 'message' => 'success');
	echo json_encode($arr);
}
else {
	echo "<h1>错误：无法识别appid，已经上传错误信息（仅上传错误信息，不会收集您设备里的资料）</h1>";
	getuser();
}
//echo "<h1>（appid：" . $_GET["appid"] . "） POWERD BY ZHETENGTIAO</h1></body></html>";
?>